package calorie.com.calorie.fragment;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lxh.baselibray.base.XFragment;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SPUtils;
import com.lxh.baselibray.util.ToastUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import calorie.com.calorie.R;
import calorie.com.calorie.activity.ActiveAccountActivity;
import calorie.com.calorie.activity.CreateAccountActivity;
import calorie.com.calorie.activity.ManagerAccountActivity;
import calorie.com.calorie.activity.PersonalInfomationActivity;
import calorie.com.calorie.activity.ReceivingInformationActivity;
import calorie.com.calorie.activity.ScanDeviceActivity;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.eventbus.EventImpl;
import calorie.com.calorie.util.NumberFormatUtils;
import calorie.com.calorie.util.StaticValue;


/**
 * 个人中心
 */
public class PersonCenterFragment extends XFragment {
    @BindView(R.id.tv_account)
    TextView tvAccount;
    @BindView(R.id.tv_level)
    TextView tvLevel;
    @BindView(R.id.tv_not_active)
    TextView tvNotActive;
    @BindView(R.id.tv_physical_strenth)
    TextView tvPhysicalStrenth;
    @BindView(R.id.progress_bar)
    ProgressBar progressBar;
    @BindView(R.id.tv_percent)
    TextView tvPercent;
    @BindView(R.id.iv_pic)
    ImageView ivPic;
    @BindView(R.id.tv_receiving_information)
    TextView tvReceivingInformation;
    Unbinder unbinder;

    @Override
    public void initData(Bundle savedInstanceState) {
        updateActiveMsg();
    }

    private void updateActiveMsg() {
        try {
            String account = SPUtils.getString(Constant.ACCOUNT, null, context);
            int status = SPUtils.getInt(Constant.STATUS, 0, context);
            String powerNum = SPUtils.getString(Constant.POWERNUM, null, context);
//            !ObjectUtils.isEmpty(StaticValue.user) && StaticValue.user.getStatus() == 1

            if (account != null && !account.equals("")) {
                if (status == 1) {
                    tvNotActive.setTextColor(context.getResources().getColor(R.color.yellow));
                    tvNotActive.setText("已激活");
                    Drawable leftDrawable = ContextCompat.getDrawable(context, R.drawable.icon_active_flag);
                    leftDrawable.setBounds(0, 0, leftDrawable.getMinimumWidth(), leftDrawable.getMinimumHeight());
                    tvNotActive.setCompoundDrawables(leftDrawable, null, null, null);
                    if (powerNum != null) {
                        double num = Double.parseDouble(powerNum) / 10000.0 * 100;
                        progressBar.setProgress((int) num);
                        tvPercent.setText(NumberFormatUtils.keepTwoDecimal(num + "") + "%");
                    }

                }

                String levelText = "";
                int resId = 0;
                switch (StaticValue.user.getLevel()) {
                    case 0:
                        levelText = "帐号未激活";
                        resId = R.drawable.icon_zero_level;
                        break;
                    case 1:
                        levelText = "菜鸟";
                        resId = R.drawable.level_cainiao;
                        break;
                    case 2:
                        levelText = "达人";
                        resId = R.drawable.level_daren;
                        break;
                    case 3:
                        levelText = "健将";
                        resId = R.drawable.level_jianjiang;
                        break;
                    case 4:
                        levelText = "精英";
                        resId = R.drawable.level_jingying;
                        break;
                    case 5:
                        levelText = "教练";
                        resId = R.drawable.level_jiaolian;
                        break;
                    case 6:
                        levelText = "大师";
                        resId = R.drawable.level_dashi;
                        break;
                    case 7:
                        levelText = "王者";
                        resId = R.drawable.level_wangzhe;
                        break;
                }
                ivPic.setImageResource(resId);
                tvLevel.setText("等级：" + levelText);
            }
            tvAccount.setText("帐户：" + SPUtils.getString(Constant.ACCOUNT, null, context));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_person_center;
    }


    @OnClick({R.id.tv_active_account, R.id.tv_create_account, R.id.tv_person_msg, R.id.tv_manager_account, R.id.tv_wristband,R.id.tv_receiving_information})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_active_account://激活账号
                //如果已经激活，就不用跳转了
                if (!ObjectUtils.isEmpty(StaticValue.user) && StaticValue.user.getStatus() == 1) {
                    ToastUtils.showLongToast(context, "账号已经激活");
                    return;
                }
                startActivity(new Intent(context, ActiveAccountActivity.class));
                break;
            case R.id.tv_create_account://创建账号
                startActivity(new Intent(context, CreateAccountActivity.class));
                break;
            case R.id.tv_person_msg://个人资料
                startActivity(new Intent(context, PersonalInfomationActivity.class));
                break;
            case R.id.tv_manager_account://账号管理
                startActivity(new Intent(context, ManagerAccountActivity.class));
                break;
            case R.id.tv_wristband://我的手环
                startActivity(new Intent(context, ScanDeviceActivity.class));
                break;
            case R.id.tv_receiving_information://收货信息
                startActivity(new Intent(context, ReceivingInformationActivity.class));
                break;
        }
    }

    @Override
    public boolean useEventBus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiveActivity(EventImpl.ActiveAccountEvent event) {
        if (!ObjectUtils.isEmpty(event)) {
            updateActiveMsg();
        }
    }


}
